﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenWings.Entities;
using OpenWings.UI.Elements;
using OpenWings.UI.Helpers;
using System.Diagnostics;

namespace OpenWings.UI.Controls
{
    public partial class ServersView : UserControl
    {
        public event EventHandler<ServerEventArgs> SelectServer;
        public event EventHandler<ServerEventArgs> EditServer;
        public event EventHandler<ServerEventArgs> AddServer;
        public event EventHandler<ServerEventArgs> DeleteServer;

        ListViewGroup _EmptyGroup;
        List<Server> _Servers;
        Server _SelectedServer;
        Client _LaunchWithClient;

        public ServersView()
        {
            InitializeComponent();
        }

        public void LaunchWithClient(Client client)
        {
            _LaunchWithClient = client;
        }

        public List<Server> Servers
        {
            get
            {
                return _Servers;
            }
            set
            {
                _Servers = value;
                PopulateListView(_Servers);
                SelectedServer = null;
            }
        }

        public Server SelectedServer
        {
            get
            {
                return _SelectedServer;
            }
            set
            {
                _SelectedServer = value;

#if DEBUG
                Trace.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name);
#endif

                if (SelectServer != null)
                    SelectServer(this, new ServerEventArgs(_SelectedServer));
            }
        }

        void PopulateListView(List<Server> servers)
        {
            ListHelper.PopulateListView(ServersListView, delegate(ListView view)
            {
                view.Items.Clear();

                if (servers != null)
                {
                    if (servers.Count == 0)
                    {
                        ListViewItem Item = new ListViewItem(_EmptyGroup);
                        Item.Text = "...";
                        view.Items.Add(Item);
                    }
                    else
                    {
                        foreach (Server OneServer in servers)
                        {
                            ListViewItemServer OneItem = new ListViewItemServer(OneServer);
                            OneItem.Group = view.Groups[OneServer.Type + "Group"];
                            view.Items.Add(OneItem);
                        }
                    }
                }
            });
        }

        public View View
        {
            get
            {
                return ServersListView.View;
            }
            set
            {
                ServersListView.View = value;
            }
        }

        public string[] GroupNames
        {
            set
            {
                ServersListView.Groups.Clear();

                foreach (string ServerTypeName in value)
                    ServersListView.Groups.Add(ServerTypeName + "Group", ServerTypeName);

                _EmptyGroup = ServersListView.Groups.Add("_Empty", "This list is empty");
            }
        }

        private void ServerListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItemServer ItemServer = e.Item as ListViewItemServer;

            if (e.IsSelected && ItemServer != null)
                SelectedServer = ItemServer.InnerEntity;
            else
                SelectedServer = null;
        }

        private void PropertiesMenuItem_Click(object sender, EventArgs e)
        {
            if (EditServer != null)
                EditServer(this, new ServerEventArgs(SelectedServer));
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            Server NewServer = new Server();
            NewServer.Name = "New client";

            ListViewItem NewItem = new ListViewItemServer(NewServer);
            ServersListView.Items.Add(NewItem);

            NewItem.Selected = true;

            if (AddServer != null)
                AddServer(this, new ServerEventArgs(NewServer));
        }

        private void ServerContextMenu_Opening(object sender, CancelEventArgs e)
        {
            DeleteMenuItem.Text = "&Delete";

            if (SelectedServer == null)
            {
                if (_LaunchWithClient == null)
                    LaunchMenuItem.Text = "Please select a server and a client";
                else
                    LaunchMenuItem.Text = "Please select a server to launch with " + _LaunchWithClient.Name;

                DeleteMenuItem.Enabled = false;
                PropertiesMenuItem.Enabled = false;
            }
            else
            {
                if (_LaunchWithClient == null)
                    LaunchMenuItem.Text = "Please select a client with " + SelectedServer.Name;
                else
                    LaunchMenuItem.Text = "&Launch " + _LaunchWithClient.Name + " on " + SelectedServer.Name;

                DeleteMenuItem.Text += " " + SelectedServer.Name;

                DeleteMenuItem.Enabled = true;
                PropertiesMenuItem.Enabled = true;
            }
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedServer == null)
                return;

            if (MessageBox.Show(this, "Are you sure you want to delete " + SelectedServer.Name + " ?", "Delete client", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (DeleteServer != null)
                    DeleteServer(this, new ServerEventArgs(SelectedServer));

                ServersListView.Items.Remove(ServersListView.SelectedItems[0]);
            }
        }

        private void LaunchMenuItem_Click(object sender, EventArgs e)
        {

        }
    }

    public class ServerEventArgs : EventArgs
    {
        public Server Server;

        public ServerEventArgs(Server server)
        {
            Server = server;
        }
    }
}
